
void f(int wid, int hei, double viswid, double vishei, double visx, double visy, int maxi, double maxm);

void FormFractal_close(Window*);

void FormFractal_dispose(Window* self);

void FormFractal_draw(Window* self);

class FormFractal : public Form
{
  public:
  DynamicObject<void>* fileBuff;
  int file_fileSize_;
  
  void init(DynamicObject<Window>* self){

    Form::init(self);
    id = 123;
    width = 600;
    height = 500;
    pos_x = 50;
    pos_y = 20;
    //b->text="timer";
    color = getRGB565(0x0000000);
    title="Fractal";
    pOnClose = FormFractal_close;

    pDispose = FormFractal_dispose;
    pDraw = FormFractal_draw;
  }

  void draw(){
    Form::draw();
    
    f(256, 192 , 2.8, 1.6, -1.1, -1.6, 255, 4);

      
    //Stream* s = getStream(5,22, 800, 600);
    //sprint(s, (const char*)fileBuff->obj);
    //print("done:");printInt(IORD(MYTIMER, MYTIMER_32));print("\r\n");
    
  }
  void dispose(){
    Form::dispose();
  }
};
void FormFractal_close(Window* window){
  windows->obj->removeWindow((DynamicObject<Window>*)formFractal);
  windows->obj->refresh();
  formFractal=0;

}

void FormFractal_dispose(Window* self){
  DynamicMem::inst->dFree(((FormFractal*)self)->fileBuff);
  ((FormFractal*)self)->dispose();
}

void FormFractal_draw(Window* self){
  ((FormFractal*)self)->draw();
}




void f(int wid, int hei, double viswid, double vishei, double visx, double visy, int maxi, double maxm){
  short* buffAddr = (short*)(((windows->obj->screenBase+formFractal->obj->abs_pos_y+4+16)<<11)+((formFractal->obj->abs_pos_x+4)<<1));

	int i;
	int j;
	int index = 0;
	int clr;
	double ReaC;
	double ImaC;
	double dReaC;
	double dImaC;
	double ReaZ;
	double ImaZ;
	double ReaZ2;
	double ImaZ2;
	//int n;

	//n = 256 / maxi;

	dReaC = (viswid) / ((double)wid - 1);
	dImaC = (vishei) / ((double)hei - 1);

	for (j = 0; j < hei; j++)
	{
    short* addr;
		for (i = 0; i < wid; i++)
		{
      addr = &(buffAddr[i+(j<<10)]);
      *addr = getRGB565(0x000000FF);//at 2Mbyte
		}
    flushCache(addr);
	}
  int t0 = IORD(MYTIMER, MYTIMER_32);
	ReaC = visy;
	for (j = 0; j < hei; j++)
	{
		ReaC += dReaC;
		ImaC = visx;
    IOWR(DEBUGGER, 2, 1);
		for (i = 0; i < wid; i++)
		{
			ImaC += dImaC;
			ReaZ = 0;
			ImaZ = 0;
			ReaZ2 = 0;
			ImaZ2 = 0;
			clr = 1;

			while (clr++ < maxi && ReaZ2 + ImaZ2 < maxm)
			{
				ReaZ2 = ReaZ * ReaZ;
				ImaZ2 = ImaZ * ImaZ;
				ImaZ = 2 * ImaZ * ReaZ + ImaC;
				ReaZ = ReaZ2 - ImaZ2 + ReaC;
			}
      short* addr0 = &(buffAddr[((i<<1)+0)+(((j<<1)+0)<<10)]);
      short* addr1 = &(buffAddr[((i<<1)+1)+(((j<<1)+0)<<10)]);
      short* addr2 = &(buffAddr[((i<<1)+0)+(((j<<1)+1)<<10)]);
      short* addr3 = &(buffAddr[((i<<1)+1)+(((j<<1)+1)<<10)]);
    
      int v1 = 0;
      int v2 = clr;
      __asm__ __volatile__("setrgb %0,%1,%2,1":"=r"(v1):"r"(v1),"r"(v2));
      *addr0 = v1;
      *addr1 = v1;
      *addr2 = v1;
      *addr3 = v1;
      
      //*addr = getpixel(0, clr, 0);// * n    at 2Mbyte
      flushCache(addr0);
      flushCache(addr1);
      flushCache(addr2);
      flushCache(addr3);
		}

    int t1 = IORD(MYTIMER, MYTIMER_32);
    printInt(j);print(",");printInt(t1-t0);print(" \r\n");
    t0 = IORD(MYTIMER, MYTIMER_32);
	}
}
